<?php

/**
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class AttachmentModel extends CommonModel {

    //array(验证字段,验证规则,错误提示,[验证条件,附加规则,验证时间])
    protected $_validate = array(
    );
    //array(填充字段,填充内容,[填充条件,附加规则])
    protected $_auto = array(
    );

    /**
     * 保存附件信息到附件表
     * @param type $userid 上传用户ID
     * @param type $catid 上传栏目ID
     * @param type $module 上传模块
     * @param type $info 附件详细信息
     * @param type $isthumb 是否缩图，1是
     */
    public function FileData($userid, $catid, $module, $info, $isthumb = 0) {
        if (!is_array($info)) {
            return false;
        }

        extract($info);

        $data = array(
            "module" => $module,
            "catid" => $catid,
            "filename" => $filename,
            "filepath" => $servaname . "/" . $filesavename,
            "filesize" => $filesize,
            "fileext" => $fileextension,
            "isimage" => in_array($fileextension, array("jpg", "png", "jpeg", "gif")) ? 1 : 0,
            "isthumb" => $isthumb,
            "userid" => $userid,
            "uploadtime" => time(),
            "uploadip" => get_client_ip(),
            "status" => 0,
            "authcode" => $filehash
        );

        $status = $this->add($data);

        return $status;
    }

    /**
     * 附件更新接口.
     * @param string $content 可传入空，html，数组形式url，url地址，传入空时，以cookie方式记录。
     * @param string 传入附件关系表中的组装id
     * @isurl intval 为本地地址时设为1,以cookie形式管理时设置为2
     */
    public function api_update($content, $keyid, $isurl = 0) {
        $keyid = trim($keyid);
        $isurl = intval($isurl);
        if ($isurl == 2 || empty($content)) {
            $this->api_update_cookie($keyid);
        } else {
            $att_index_db = M("Attachment_index");
            //http附件地址
            $upload_url = CONFIG_SITEFILEURL;
            if (strpos($upload_url, '://') !== false) {
                $pos = strpos($upload_url, "/", 8);
                $domain = substr($upload_url, 0, $pos) . '/';
                $dir_name = substr($upload_url, $pos + 1);
            }
            if ($isurl == 0) {
                $pattern = '/(href|src)=\"(.*)\"/isU';
                preg_match_all($pattern, $content, $matches);
                if (is_array($matches) && !empty($matches)) {
                    $att_arr = array_unique($matches[2]);
                    foreach ($att_arr as $_k => $_v)
                        $att_arrs[$_k] = md5(str_replace(array($domain, $dir_name), '', $_v));
                }
            } elseif ($isurl == 1) {
                if (is_array($content)) {
                    $att_arr = array_unique($content);
                    foreach ($att_arr as $_k => $_v)
                        $att_arrs[$_k] = md5(str_replace(array($domain, $dir_name), '', $_v));
                } else {
                    $att_arrs[] = md5(str_replace(array($domain, $dir_name), '', $content));
                }
            }
            $att_index_db->where(array('keyid' => $keyid))->delete();
            if (is_array($att_arrs) && !empty($att_arrs)) {
                foreach ($att_arrs as $r) {
                    $infos = $this->where(array('authcode' => $r))->getField("aid,aid");
                    if ($infos) {
                        $this->where(array('aid' => $infos['aid']))->save(array('status' => 1));
                        $att_index_db->add(array('keyid' => $keyid, 'aid' => $infos['aid']));
                    }
                }
            }
        }
        //删除附件cookie
        AppframeAction::cookie('att_json', NULL);
        return true;
    }

    /*
     * cookie 方式关联附件
     */

    private function api_update_cookie($keyid) {

        $att_index_db = M("Attachment_index");
        $att_json = AppframeAction::cookie('att_json');
        if ($att_json) {
            $att_cookie_arr = explode('||', $att_json);
            $att_cookie_arr = array_unique($att_cookie_arr);
        } else {
            return false;
        }
        foreach ($att_cookie_arr as $_att_c)
            $att[] = json_decode($_att_c, true);
        foreach ($att as $_v) {
            $this->where(array('aid' => $_v['aid']))->save(array('status' => 1));
            $att_index_db->add(array('keyid' => $keyid, 'aid' => $_v['aid']));
        }
    }

    /*
     * 附件删除接口
     * @param string 传入附件关系表中的组装id
     */
    public function api_delete($keyid) {
        $keyid = trim($keyid);
        if ($keyid == '')
            return false;
        $att_index_db = M("Attachment_index");
        $info = $att_index_db->where(array('keyid' => $keyid))->getField("aid,aid");
        if ($info) {
            $att_index_db->where(array('keyid' => $keyid))->delete();
            foreach ($info as $_v) {
                if (!$att_index_db->where(array('aid' => $_v['aid']))->find()) {
                    $this->where(array('aid' => $_v['aid']))->delete();
                }
            }
            return true;
        } else {
            return false;
        }
    }

}

?>
